import Dialog from "../../miniprogram_npm/@vant/weapp/dialog/dialog";
const app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    propA: String,
    propKeyword: String
  },

  /**
   * 组件的初始数据
   */
  data: {
    salaryList: []
  },

  // 组件生命周期函数-在组件实例进入页面节点树时执行)
  attached() {
    this.init();
  },

  pageLifetimes: {
    show: function () {
      this.init();
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      this.setData({
        salaryList: []
      });
      const { propA, propKeyword } = this.data;
      let params = {
        asc: true,
        columns: [],
        companyId: app.globalData.userInfo.companyData.companyId,
        current: 1,
        currentMongo: 0,
        keyword: propKeyword,
        size: 500
        // "entTime": "",
        // "settleStatus": "",
        // "startTime": "",
        // "userId": app.globalData.userInfo.userDetail.id
      };
      if (propA == "1") {
        // 未结账
        wx.$apis.getSalaryManagementPage(params).then((res) => {
          wx.stopPullDownRefresh();
          if (res.data) {
            this.setData({
              salaryList: res.data.records
            });
          }
        });
      } else {
        // 已结账
        wx.$apis.getSettlementSalaryStatistical(params).then((res) => {
          wx.stopPullDownRefresh();
          if (res.data) {
            this.setData({
              salaryList: res.data.records.map((item) => {
                return {
                  ...item,
                  settlementAmount: wx.$getYuan(item.settlementAmount)
                };
              })
            });
          }
        });
      }
    },
    toSalaryManagement(e) {
      let userId = e.currentTarget.dataset.id;
      let userName = e.currentTarget.dataset.name;
      let mobile = e.currentTarget.dataset.mobile;
      if (this.data.propA == "1") {
        wx.navigateTo({
          url: "/pages/management/unsettled/index?id=" + userId + "&name=" + userName + "&mobile=" + mobile
        });
      } else {
        wx.navigateTo({
          url: "/pages/management/closed-list/index?id=" + userId + "&name=" + userName + "&mobile=" + mobile
        });
      }
    },
    onDelete(event) {
      const id = event.target.dataset.id;
      Dialog.confirm({
        message: "确定删除吗？"
      }).then(() => {
        this.delete(id);
      });
    },
    // 删除亦是解绑
    delete(id) {
      wx.showLoading({
        title: "删除中..."
      });
      wx.$apis
        .deleteMarketUser({
          marketUserIds: [id],
          marketRoleId: 2
        })
        .then((res) => {
          this.getData();
          if (!res.data) {
            return wx.showToast({
              title: "删除失败，请稍后再试~",
              icon: "none",
              duration: 2000
            });
          }
          wx.showToast({
            title: "删除成功",
            icon: "success",
            duration: 2000
          });
        });
    }
  }
});
